#if false
#region License, Terms and Conditions
//
// Jayrock - JSON and JSON-RPC for Microsoft .NET Framework and Mono
// Written by Atif Aziz (www.raboof.com)
// Copyright (c) 2005 Atif Aziz. All rights reserved.
//
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License as published by the Free
// Software Foundation; either version 3 of the License, or (at your option)
// any later version.
//
// This library is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
// details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this library; if not, write to the Free Software Foundation, Inc.,
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
//
#endregion

namespace Jayrock.Json.Conversion.Generation
{
    #if !NET_1_0 && !NET_1_1 && !NET_2_0

    using System;

    public static class ImportResult
    {
        public static ImportResult<T> Success<T>(T value) { return ImportResult<T>.ForValue(value); }
        public static ImportResult<T> Error<T>() { return Error<T>(null); }
        public static ImportResult<T> Error<T>(Exception e) { return ImportResult<T>.ForError(e); }
    }

    [Serializable]
    public struct ImportResult<T>
    {
        readonly Exception _error;
        readonly T _value;

        public bool Success { get; private set; }
        public T Value { get { GateSuccess(true); return _value; } }
        public Exception Error { get { GateSuccess(false); return _error; } }

        ImportResult(bool success, T value, Exception error) : this()
        {
            _value = value;
            _error = error;
            Success = success;
        }

        public Importer<T> Return() { return JsonImport.Return(this); }

        public ImportResult<TResult> Bind<TResult>(Func<T, ImportResult<TResult>> binder)
        {
            if (binder == null) throw new ArgumentNullException("binder");
            return Success 
                 ? binder(Value) 
                 : ImportResult.Error<TResult>(Error);
        }

        void GateSuccess(bool expected) { if (Success != expected) throw new InvalidOperationException(); }

        public static ImportResult<T> ForValue(T value) { return new ImportResult<T>(true, value, default(Exception)); }
        public static ImportResult<T> ForError(Exception e) { return new ImportResult<T>(false, default(T), e ?? new JsonException(string.Format(@"Error importing an instance of {0} from JSON source.", typeof(T)))); }
    }

    #endif // !NET_1_0 && !NET_1_1 && !NET_2_0
}
#endif
#region License, Terms and Conditions
//
// Jayrock - JSON and JSON-RPC for Microsoft .NET Framework and Mono
// Written by Atif Aziz (www.raboof.com)
// Copyright (c) 2005 Atif Aziz. All rights reserved.
//
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License as published by the Free
// Software Foundation; either version 3 of the License, or (at your option)
// any later version.
//
// This library is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
// details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this library; if not, write to the Free Software Foundation, Inc.,
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
//
#endregion

namespace Jayrock.Json.Conversion.Generation
{
    #if !NET_1_0 && !NET_1_1 && !NET_2_0
    
    using System;

    [Serializable]
    public class ImportState
    {
        Exception _error;

        public bool IsInitialized { get; private set; }
        public JsonReader Input { get; private set; }

        ImportState(JsonReader input)
        {
            Input = input;
        }

        public bool Success { get { GateState(true); return _error == null; } }
        public Exception Error { get { GateState(_error != null); return _error; } }
        void GateState(bool valid) { if (!IsInitialized || !valid) throw new InvalidOperationException(); }

        ImportState EnterSuccess() { return Enter(null); }
        ImportState EnterError(Exception e) { return Enter(e); }
        
        ImportState Enter(Exception e)
        {
            IsInitialized = true;
            _error = e;
            return this;
        }

        T Setting<T>(Exception error, T value)
        {
            Enter(error);
            return value;
        }

        public static ImportState Create(JsonReader reader) { return new ImportState(reader); }
        public static ImportState CreateSuccess(JsonReader reader) { return new ImportState(reader).EnterSuccess(); }
        public static ImportState CreateError(JsonReader reader) { return CreateError(reader, null); }
        public static ImportState CreateError(JsonReader reader, Exception e) { return new ImportState(reader).EnterError(e ?? /*TODO*/new JsonException()); }

        public T Succeeding<T>(T value) { return Setting(null, value); }
        public T Failing<T>() { return Failing<T>(null); }
        public T Failing<T>(Exception e) { return Setting(e ?? new JsonException(string.Format(@"Error importing an instance of {0} from JSON source.", typeof(T))), default(T)); }

        public TResult Bind<T, TResult>(T value, Func<T, TResult> binder)
        {
            if (binder == null) throw new ArgumentNullException("binder");
            return Success
                 ? binder(value)
                 : Failing<TResult>(Error);
        }
    }

    #endif // !NET_1_0 && !NET_1_1 && !NET_2_0
}
